<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '400px'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: '水果销量统计',
          left: 'center',
          top: 20,
          textStyle: {
            color: '#555555'
          }
        },
        tooltip: {},
        xAxis: {
          data: [
            "苹果",
            "香蕉",
            "橘子",
            "火龙果",
            "葡萄",
            "西瓜"
          ]
        },
        yAxis: {},
        series: [{
          name: '销量',
          type: 'bar',
          data: [
            {
              value: 333,
              itemStyle: {
                color: "#3fb1e3"
              }
            },
            {
              value: 133,
              itemStyle: {
                color: "#c4ebad"
              }
            },
            {
              value: 99,
              itemStyle: {
                color: "#c4ebad"
              }
            },
            {
              value: 222,
              itemStyle: {
                color: "#6be6c1"
              }
            },
            {
              value: 399,
              itemStyle: {
                color: "#3fb1e3"
              }
            },
            {
              value: 123,
              itemStyle: {
                color: "#c4ebad"
              }
            }
          ]
        }]
      })
    }
  }
}
</script>
